﻿using System.ComponentModel.DataAnnotations;

namespace UserRegistration.Model.DP
{
    public class UserName
    {
        private readonly string name;

        public UserName(string name)
        {
            IsValid(name);
            this.name = name;
        }

        public string Name => name;

        private void IsValid(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ValidationException("name 不能为空");
            }
            if (name.Length > 20)
            {
                throw new ValidationException("name 长度不能超过20");
            }
            if (name.Contains("特殊字符"))
            {
                throw new ValidationException("name 不能包括特殊字符");
            }
        }
    }
}